﻿using UnityEngine;
using UnityEngine.UI;
using static CyberU;
using static CyberU.UI;
public class CanvasTransition : MonoBehaviour {
    private RectTransform canvasRect;
    private Vector2 startPos;
    private Vector2 endPos;
    private Quaternion startRot;
    private Quaternion endRot;
    private float duration;
    private float elapsedTime;
    private bool isAnimating;

    public void Awake()
    {
        canvasRect = AsRectTransform(gameObject);
    }
    public void StartTransition(Vector3 toPos, Quaternion toRot, float animDuration) {
        if (transform == null) return;

        startPos = transform.position;
        endPos = toPos;
        startRot = transform.rotation;
        endRot = toRot;
        duration = animDuration;
        elapsedTime = 0f;
        isAnimating = true;
    }

    void Update() {
        if (!isAnimating || transform == null) return;
        
        elapsedTime += Time.deltaTime;
        float t = Mathf.Clamp01(elapsedTime / duration);
        
        // 3D 位置插值
        transform.position = Vector3.Lerp(startPos, endPos, t);
        // 四元数旋转插值（用 Lerp 或 Slerp）
        transform.rotation = Quaternion.Slerp(startRot, endRot, t);

        if (elapsedTime >= duration) {
            transform.position = endPos;
            transform.rotation = endRot;
            isAnimating = false;
        }
    }

    public void StopTransition() {
        isAnimating = false;
    }
}